<?php
/**
 * Created by PhpStorm.
 * User: chl
 * Date: 2017/4/1 0001
 * Time: 16:28
 */
namespace app\admin\controller;
use app\models\Bases;

class Config extends Base{
    public function base(){
        $config = new Bases('config');
        if(request()->isAjax()){
            $post = input('param.');
            try{
                // 获取配置与表单循环对比
                $clist = $config->selectData();
                if(!$clist){
                    throw new \Exception('操作失败',1010);
                }
                $str = '';
                foreach($post as $k => $v){
                    foreach($clist as $v1){
                        if($k == $v1['cname'] && $v != $v1['option']){
                            // 有修改的存入数据库，并记录修改内容存入日志表
                            $config->setField(array('cname' => $k),array('option' => $v));
                            $str .= "{$v1['name']}：'{$v1['option']}' 修改为 '$v',";
                        }
                    }
                }
                if($str){
                    $config->addAdminLog(rtrim($str,','));
                }
            } catch (\LogicException $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        } else {
            $clist = $config->selectData();
            if(!$clist){
                throw new \Exception('操作失败',1010);
            }
            $arr = [];
            foreach($clist as $v){
                $arr[$v['cname']] = $v['option'];
            }
            return view('base',['info'=>$arr]);
        }
    }
}